package com.example.demo.service.Impl;

import com.example.demo.mapper.HellLogMapper;
import com.example.demo.mapper.LifebookMapper;
import com.example.demo.pojo.HellLog;
import com.example.demo.pojo.Lifebook;
import com.example.demo.service.HellLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class HellLogServiceImpl implements HellLogService {

    @Autowired
    private HellLogMapper hellLogMapper;
    @Autowired
    private LifebookMapper lifebookMapper;
    // 地狱受刑总数
    @Override
    public int queryHellLogCount() {
        return hellLogMapper.qureyHellLogCount();
    }
    // 地狱受刑添加
    @Override
    public String  HellLogAdd(HellLog hellLog) {
        Lifebook lifebook = lifebookMapper.selectByid(Integer.parseInt(hellLog.getUid()));
        if (lifebook.getPunishmentid() != null) {
            return "添加失败，该用户已在地狱中受刑";
        }else {
            hellLog.setCreate_time(LocalDateTime.now());
            hellLogMapper.HellLogAdd(hellLog);
            HellLog hh = hellLogMapper.HellLogQueryBYuid(hellLog.getUid());
            lifebook.setPunishment("服刑" + hh.getTime());
            lifebook.setPunishmentid(hh.getId());
            lifebookMapper.updateLifeBook(lifebook);
            return "添加成功";
        }
    }
    //地狱受刑列表查询
    @Override
    public PageInfo HellLogQuery(Integer page, Integer limit, HellLog hellLog) {
        //    1.获取总记录数
        PageHelper.startPage(page,limit);
        //    2.执行操作
        List<HellLog> list = hellLogMapper.HellLogQuery(hellLog);
        PageInfo<HellLog> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    //地狱受刑删除
    @Override
    public void HellLogDelete(Integer id) {
        hellLogMapper.HellLogDelete(id);
    }
    //地狱受刑修改
    @Override
    public void HellLogUpdate(HellLog hellLog) {
        hellLog.setCreate_time(LocalDateTime.now());
        hellLogMapper.HellLogUpdate(hellLog);
    }
}
